﻿using System;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Events.Bus.Handlers;
using DHJ.Cost.CostBooking.Event;

namespace DHJ.Cost.CostAccounting
{
    public class CostAllocator : IEventHandler<CostAccounted>, ITransientDependency
    {
        private readonly IRepository<AllocRel, Guid> _repository;

        public CostAllocator(IRepository<AllocRel, Guid> repository)
        {
            _repository = repository;
        }

        public void HandleEvent(CostAccounted eventData)
        {
            foreach (var r in eventData.Ratios)
            {
                _repository.Insert(new AllocRel(eventData.Cost.Id, r.BatchId, r.Quantity, r.Workshop, r.Ratio));
            }
        }
    }
}